<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, id = this.$get('id'), btns = {
      '拒绝'(){
        app.config.status.value = 2;
        app.$confirm('是否确定拒绝此申请').then(()=>{
          app.$refs['forms'].submit();
        }).catch(()=>{})
      },
      '同意_primary'(){
        app.config.status.value = 1;
        app.$confirm('是否确定同意此申请 <br/> 同意申请后，会以此申请的资料添加代理商，如果该用户之前已经是代理商的情况下，会覆盖原来的数据',{dangerouslyUseHTMLString:true}).then(()=>{
          app.$refs['forms'].submit();
        }).catch(()=>{})
      },
    };
    let forms_config = {
      user_info: {label:'申请用户',name:'user_info',tips:''},
      admin_role_ids: {label:'管理员角色',name:'admin_role_ids',type:'select_more',config:[],tips:'通过申请后，赋予代理商管理员账号的角色'},
      timeout: {label:'授权时长',name:'timeout',type:'duration_info',config:'永久|0',tips:'通过申请后，赋予的代理商时长'},
      
      title: {label:'代理商标题',name:'title',tips:''},
      head_img: {label:'头图',name:'head_img',type:'file_img',tips:''},
      agent_category_ids: {label:'代理类目',name:'agent_category_ids',type:'select_more',config:[],tips:''},
      keyword: {label:'关键词',name:'keyword',tips:''},
      info: {label:'简介',name:'info',type:'text_more',tips:''},
      area: {label:'所在地区',name:'area',tips:''},
      position: {label:'详细位置',name:'position',tips:''},
      link_name: {label:'联系人姓名',name:'link_name',tips:''},
      link_phone: {label:'联系人电话',name:'link_phone',tips:''},
      must_file: {label:'执照图片',name:'must_file',type:'file_imgs',tips:''},
      file: {label:'附件',name:'file',type:'file_imgs',tips:''},
      extend_data: {label:'备注参数',name:'extend_data',type:'json_param',tips:''},
      
      action_time: {label:'审核时间',name:'action_time'},
      create_time: {label:'申请时间',name:'create_time'},
      
      status: {label:'状态',name:'status',type:'hidden',value:0},
      admin_note: {label:'审核备注',name:'admin_note',type:'text_more',value:''},
    }

    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/agent_apply/edit?id=`+this.id : '/admin/agent_apply/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('agent_apply/index', '$refs.list_url.list');
          iframe.hide('agent_apply/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/agent_apply/info?id='+this.id, cache:true}, (res)=>{
        res.data.user_info = res.data.nickname+'(ID='+res.data.user_id+')';
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/category/list',cache:true},function(res){
      let category_config = app.decode_config(res.data.list, {content_key:'name', value_key:'id'});
      app.config.agent_category_ids.config = category_config;
      return false;
    })
    ajax({url:'/admin/admin_role/list',cache:true},function(res){
      let admin_role_config = app.decode_config(res.data.list, {content_key:'name', value_key:'id'});
      app.config.admin_role_ids.config = admin_role_config;
      return false;
    })
  }
}
</script>
